package com.star.star.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.star.common.enums.AuditStatus;
import com.star.star.dto.AuditDTO;
import com.star.star.dto.StarPhotoSaveBatchWxuserDTO;
import com.star.star.entity.StarArt;
import com.star.star.entity.StarPhoto;
import com.star.star.mapper.StarPhotoMapper;
import com.star.star.service.StarPhotoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.star.service.StarService;
import com.star.star.vo.StarPhotoListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 艺人照片 服务实现类
 *
 * @author yangzj
 */
@Service
public class StarPhotoServiceImpl extends ServiceImpl<StarPhotoMapper, StarPhoto> implements StarPhotoService {

    @Autowired
    private StarService starService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void auditBatch(List<AuditDTO> arts) {
        if (CollUtil.isEmpty(arts)) {
            return;
        }
        List<StarPhoto> collect = arts.stream().map(c -> new StarPhoto().setId(c.getId())
                .setAuditStatus(c.getAuditStatus())
                .setAuditReason(c.getAuditReason())
                .setAuditTime(LocalDateTime.now())).collect(Collectors.toList());
        this.updateBatchById(collect);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveBatchWxuser(StarPhotoSaveBatchWxuserDTO dto, Long userId) {
        Long starId = dto.getStarId();
        // 校验starId是否为当前用户，防止越权
        this.check(starId, userId);
        // 判断原来是否有封面了
        boolean exists = this.lambdaQuery().eq(StarPhoto::getStarId, starId).eq(StarPhoto::getIsCover, true).exists();
        AtomicInteger i = new AtomicInteger(0);
        List<StarPhoto> list = dto.getPhotoUrls().stream().map(c -> {
            // 如果原来没有设置第一张为封面
            boolean isCover = !exists && i.getAndIncrement() == 0;
            return new StarPhoto().setStarId(starId)
                    .setIsCover(isCover)
                    .setPhotoUrl(c)
                    .setAuditStatus(AuditStatus.WAIT.getCode())
                    .setIsPublish(false);
        }).collect(Collectors.toList());
        this.saveBatch(list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePublishWxuser(Long id, Boolean isPublish, Long userId) {
        StarPhoto starPhoto = Assert.notNull(this.getById(id), "照片不存在");
        Assert.isTrue(AuditStatus.OK.getCode().equals(starPhoto.getAuditStatus()), "审核通过后才能发布");
        this.check(starPhoto.getStarId(), userId);
        this.updateById(new StarPhoto().setId(id).setIsPublish(isPublish).setPublishTime(LocalDateTime.now()));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCoverWxuser(Long id, Long userId) {
        StarPhoto starPhoto = Assert.notNull(this.getById(id), "照片不存在");
        this.check(starPhoto.getStarId(), userId);
        // 取消原封面
        this.update(Wrappers.<StarPhoto>lambdaUpdate().eq(StarPhoto::getStarId, starPhoto.getStarId()).
                set(StarPhoto::getIsCover, false));
        this.updateById(new StarPhoto().setId(id).setIsCover(true));

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIdWxuser(Long id, Long userId) {
        StarPhoto starPhoto = Assert.notNull(this.getById(id), "照片不存在");
        this.check(starPhoto.getStarId(), userId);
        this.removeById(id);
    }

    @Override
    public List<StarPhotoListVO> listByStarId(Long starId, Boolean isPublish, Integer auditStatus, Long userId) {
        return baseMapper.listByStarId(starId, isPublish, auditStatus, userId);
    }

    /**
     * 检查
     *
     * @param starId 明星ID
     * @param userId 用户ID
     */
    private void check(Long starId, Long userId) {
        Assert.isTrue(starService.exists(starId, userId), "当前艺人不存在");
    }
}
